package top.yaofengqiao.springcloudsimple.auth.biz.trigger.http;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.yaofengqiao.springcloudsimple.auth.biz.domain.auth.model.valobj.Oauth2Type;
import top.yaofengqiao.springcloudsimple.auth.biz.domain.auth.model.valobj.UserType;
import top.yaofengqiao.springcloudsimple.auth.biz.domain.auth.service.IAuthService;
import top.yaofengqiao.springcloudsimple.auth.biz.trigger.http.dto.Oauth2LoginDTO;
import top.yaofengqiao.springcloudsimple.auth.biz.trigger.http.dto.RefreshTokenDTO;
import top.yaofengqiao.springcloudsimple.auth.biz.trigger.http.dto.UsernameLoginDTO;
import top.yaofengqiao.springcloudsimple.auth.biz.trigger.http.vo.OauthTokenVO;
import top.yaofengqiao.springcloudsimple.common.constant.AuthConstant;
import top.yaofengqiao.springcloudsimple.common.constant.CommonConstant;
import top.yaofengqiao.springcloudsimple.common.constant.ResponseCode;
import top.yaofengqiao.springcloudsimple.common.util.AppException;
import top.yaofengqiao.springcloudsimple.common.util.R;

import javax.annotation.Resource;

/**
 * @author yfq
 * @date 2024/6/4 16:30
 * @description
 */
@RestController
@RequestMapping("/oauth")
public class OauthController {
    @Resource
    private IAuthService authService;

    /**
     * 用户名密码登录
     */
    @PostMapping("/usernameLogin")
    public R<OauthTokenVO> usernameLogin(@RequestBody UsernameLoginDTO usernameLoginDTO,
                                         @RequestHeader("User-Type") int userType) {
        UserType type = UserType.determineUserType(userType);

        if (type == null) throw new AppException(ResponseCode.USER_TYPE_NOT_EXIST);

        OauthTokenVO oauthTokenVO = authService.usernameLogin(usernameLoginDTO, type);

        return R.ok(oauthTokenVO);
    }

    /**
     * 三方授权登录
     */
    @PostMapping("/oauth2Login")
    public R<OauthTokenVO> oauth2Login(@RequestBody Oauth2LoginDTO oauth2LoginDTO) {
        Oauth2Type type = Oauth2Type.determineOauth2Type(oauth2LoginDTO.getType());

        if (type == null) throw new AppException(ResponseCode.LOGIN_MODE_NO_SUPPORT);

        OauthTokenVO oauthTokenVO = authService.oauth2Login(oauth2LoginDTO, type);

        return R.ok(oauthTokenVO);
    }

    /**
     * 获取三方授权登录url地址
     */
    @GetMapping("/oauth2LoginUrl/{oauth2Type}")
    public R<String> oauth2LoginUrl(@PathVariable("oauth2Type") int oauth2Type) {
        Oauth2Type type = Oauth2Type.determineOauth2Type(oauth2Type);

        if (type == null) throw new AppException(ResponseCode.LOGIN_MODE_NO_SUPPORT);

        String url = authService.oauth2LoginUrl(type);

        return R.ok(url);
    }

    /**
     * 刷新token
     */
    @PostMapping("/refreshToken")
    public R<OauthTokenVO> refreshToken(@RequestBody @Validated RefreshTokenDTO refreshTokenDTO) {
        String refreshToken = refreshTokenDTO.getRefreshToken().replace(AuthConstant.BEARER + CommonConstant.BLANK, "");
        refreshTokenDTO.setRefreshToken(refreshToken);
        return R.ok(authService.refreshToken(refreshTokenDTO));
    }

}
